package com.zyh.server.controller;

import com.zyh.server.config.Constant;
import com.zyh.server.utils.R;
import com.zyh.server.utils.RedisUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "token续签")
@Slf4j
@RestController
@RequestMapping("/token")
@CrossOrigin(origins = "*")
public class TokenController {

	@Autowired
	private RedisUtil redisUtil;

	@Operation(summary = "续签方法")
	@Parameters({@Parameter(name = "token", description = "用户唯一标识", in = ParameterIn.HEADER)})
	@GetMapping("/addDuration")
	public R addDuration(@RequestHeader("token") String token) {
		log.info("获取请求头上的token值:{}", token);
		long time1 = redisUtil.getExpire(Constant.REDIS_TOKEN_PREFIX + token);
		log.info("续签之前,缓存失效时间:{}", time1);
		//每次续签30分钟
		redisUtil.expire(Constant.REDIS_TOKEN_PREFIX + token, Constant.REDIS_TOKEN_EXPIRE);
		long time2 = redisUtil.getExpire(Constant.REDIS_TOKEN_PREFIX + token);
		log.info("续签之后,缓存失效时间:{}", time2);
		return R.ok();
	}
}
